﻿Imports System.Data.SqlClient

Public Class Categoryinfo
    Dim cmd As New SqlCommand
    Public Sub refreshdata()
        grdCategory.Rows.Clear()
        cmd.Connection = cn
        cmd.CommandText = "select * from tblcategory where CatName like N'%" & txtsearch.Text & "%' OR CatID like N'%" & txtsearch.Text & "%'"
        Dim rd As SqlDataReader = cmd.ExecuteReader
        While rd.Read = True
            Me.grdCategory.Rows.Add(rd(0), rd(1), rd(2))
        End While
        rd.Close()
        radioname.Checked = True
    End Sub
    Private Sub Categoryinfo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        condb()
        refreshdata()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        If txtcategoryname.Text = "" Then
            MessageBox.Show("Please input Category name", "", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
            txtcategoryname.Focus()
            Exit Sub
        End If
        cmd.Connection = cn
        cmd.CommandText = "insert into tblcategory values(N'" & txtcategoryname.Text & "',N'" & txtdescription.Text & "')"
        cmd.ExecuteNonQuery()
        refreshdata()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If Me.grdCategory.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        If MessageBox.Show("Are you want to delete", "Delete Confirme", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If
        For Each row In grdCategory.SelectedRows
            Dim id = row.Cells("catid").Value
            cmd.Connection = cn
            cmd.CommandText = "delete from tblcategory where catID= " & id
            cmd.ExecuteNonQuery()
        Next
        refreshdata()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        cmd.Connection = cn
        cmd.CommandText = "update Tblcategory set catName=N'" & txtcategoryname.Text & "',description='" & txtdescription.Text & "' where catID='" & txtcatid.Text & "';"
        cmd.ExecuteNonQuery()
        MessageBox.Show("Update Succes")
        refreshdata()
    End Sub

    Private Sub grdCategory_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdCategory.SelectionChanged
        If Me.grdCategory.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim row As DataGridViewRow = Me.grdCategory.SelectedRows(0)
        txtcatid.Text = row.Cells("catid").Value
        txtcategoryname.Text = row.Cells("catname").Value
        txtdescription.Text = row.Cells("discription").Value
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        txtcategoryname.Clear()
        txtdescription.Clear()
        txtcatid.Clear()
    End Sub

    Private Sub txtsearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsearch.TextChanged
        refreshdata()
    End Sub

    Private Sub grdCategory_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdCategory.CellContentClick

    End Sub
End Class